// 开发环境开关
const ON = true;
const serverPath = ON
  ? "https://blogserver.blueluck.cn/"
  : "http://127.0.0.1:3000/";

import axios from "axios";
const axiosInstance = axios.create({
  baseURL: serverPath + "api/app_api/",
});
// 用户 模型 api 接口
const path = {
  user: "user/",
  // 轮播图 模型 api 接口
  carousel: "carousel/",
  // 文章 模型 api 接口
  article: "article/",
  // 评论 模型 api 接口
  comment: "comment/",
  // 分类 模型 api 接口
  category: "blog_categoryApi/",
  // 关注 模型 api 接口
  followed:"user_followsApi/",
  // 文件 / 上传 接口
  upload: "image/upload/",
  // 文件 / 下载 接口
  download: "image/download/",
  // 文件 / 删除 接口
  delete:"image/delete/",
    // 文件 / 更新 接口
  update:"image/update/",
  // QQ邮箱 STMP 接口
  sendEmail: "sendEmail/",
  // 数据删除 接口
  dataDelete:"delete/"
};
const dataDelete = async (type,date)=>{
  const data = {
    type,
    data:date
  }
  const response = await axiosInstance.post(path.dataDelete,data);
  return response.data
}
// 图片更新
const updateImageApi = async (dataValue,oldName)=>{
  console.log("api")
  console.log(oldName)
  const formData = new FormData()
  const file = dataValue
  formData.append('file', file)
  formData.append('oldFilName', oldName)
  const API = {
    post: async () => {
      const data = {
        headers: {
          'Content-Type': 'multipart/form-data'
        }
    }
      const response = await axiosInstance.post(path.update,formData,data);
      return response.data;
    }
  }
  return await API.post();
}
// 图片删除
const deleteImageApi = async (fileName)=>{
  const data = {
    fileName
  }
  const response = await axiosInstance.post(path.delete,data)
  return response.data
}
// 文件类型检测
const imageType = (value)=>{
  const allowedTypes = ['image/jpeg', 'image/png', 'image/gif']
  if (!allowedTypes.includes(value.type)) {
    return {
      type:"err",
      message:"type"
    }
  }
  const maxSizeInBytes = 5 * 1024 * 1024 
  if (value.size > maxSizeInBytes) {
    return {
      type:"err",
      message:"size"
    }
  }
  return true
}
// 图片上传
const UploadImageApi = async (value)=>{

  const formData = new FormData()
  const file = value
  formData.append('file', file)
  const API = {
    post: async () => {
      const data = {
        headers: {
          'Content-Type': 'multipart/form-data'
        }
    }
      const response = await axiosInstance.post(path.upload,formData,data);
      return response.data;
    }
  }
  return await API.post();
}

// 封装api
const axiosApi = async (pathName, data, key) => {
  const API = {
    post: async () => {
      const response = await axiosInstance.post(path[pathName] + "post", data);
      return response.data;
    },
    get: async () => {
      const response = await axiosInstance.get(path[pathName] + "get", {
        params: data,
      });
      return response.data;
    },
  };
  return await API[key]();
}
const fileApi = {
  upload : UploadImageApi,
  imageType :imageType,
  delete :deleteImageApi,
  update:updateImageApi,
}

export { fileApi,axiosApi, serverPath,dataDelete };
